package com.example.demos.service.impl;

import com.example.demos.dto.MenuConDTO;
import com.example.demos.dto.MenuDTO;
import com.example.demos.dto.MenuListDTO;
import com.example.demos.dto.UpdateRoleMenusDTO;
import com.example.demos.entity.Role;
import com.example.demos.mapper.RolePerMapper;
import com.example.demos.service.RolePerService;
import com.example.demos.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service

public class RolePerServiceImpl implements RolePerService {
    @Autowired
    private RolePerMapper rolePerMapper;
    @Override
    public PageBean<List<Role>> getRoleList(int page, int rows) {
        Page<Role> page1 = PageHelper.startPage(page, rows);
        rolePerMapper.getRoleList();
        return new PageBean<>(page1.getTotal(), page1.getResult());
    }

    @Override
    public MenuListDTO getMenuList(BigInteger id) {
        List<MenuConDTO> menuAllList = rolePerMapper.getMenuAllList();
        List<BigInteger> menuListIds = rolePerMapper.getMenuListById(id);

        return new MenuListDTO(menuAllList, menuListIds) ;
    }

    @Override
    public void updateRoleMenus(UpdateRoleMenusDTO updateRoleMenus) {
        if(updateRoleMenus.getCheckedStr().isEmpty()){
            rolePerMapper.deleteRoleAllMenusById(updateRoleMenus.getId());
        }else {
            List<BigInteger> turn = new ArrayList<>();
            rolePerMapper.deleteRoleAllMenusById(updateRoleMenus.getId());
            for (Integer i : updateRoleMenus.getCheckedStr()) {
                turn.add(new BigInteger(i.toString()));
            }
            for (BigInteger bigInteger : turn) {
                rolePerMapper.updateRoleMenus(updateRoleMenus.getId(), bigInteger);
            }
        }
    }
}
